<?php

class Card_Model extends CI_Model {
	
	function Card_Model() {
		parent::__construct ();
		$this->load->database ();
	}
	
	function get($id = FALSE) {
		if ($id === FALSE) {
			$query = $this->db->get ( 'card' );
			if ($query->num_rows () <= 0) {
				return FALSE;
			}
			return $query->result_array ();
		} else {
			$query = $this->db->get_where ( 'card', array ('id' => $id ) );
			if ($query->num_rows () <= 0) {
				return FALSE;
			}
			return $query->row_array ();
		}
	}
	
function add($data) {

        if (!isset($data['name']) || strlen($data['name']) < 3 || strlen($data['name']) > 30
        ) {
            return 0;
        }
        foreach ($data as &$d) {
            $d = $this->db->escape_str($d);
        }
        if(!isset($data['image'])) $data['image']= DEFAULTCARDIMAGE;

        $this->db->where('name', $data['name']);
        $query = $this->db->get('card');
        if ($query->num_rows() > 0)
            return false;

        if (!$this->db->insert('card', $data))
            return 0;
        else {
            return $this->db->insert_id();
        }
    }

    /*
     * 
     */

    function delete($id) {
        $this->db->where('id', $id);
        $query = $this->db->delete('card');
        if (!$query)
            return false;
        else
            return true;
    }

    /* Function: Update
     * Author: Chung HD
     * Date: 27/09/2011
     */

    function update($id, $data) {
        foreach ($data as &$d) {
            $d = $this->db->escape_str($d);
        }

        if (isset($data['name']))
            $newdata['name'] = $data['name'];
        if (isset($data['price']))
            $newdata['price'] = $data['price'];
        if (isset($data['image']))
            $newdata['image'] = $data['image'];
        if (isset($data['type']))
        	$newdata['type'] = $data['type'];
        if (isset($data['status']))
        	$newdata['status'] = $data['status'];
        $this->db->where('id', $id);
        $query = $this->db->get('card');
        if ($query->num_rows() == 0)
            return false;
        else {
            $this->db->where('id', $id);
            if ($this->db->update('card', $newdata))
                return true;
            else
                return false;
        }
    }
	
/*
     * Function: searchcardByLetter($letter)
     * Author: Chung Hd
     * Date: 28/09/2011
     */
}

?>